package com.xiaoyun.controller.center;

import com.xiaoyun.constant.Const;
import com.xiaoyun.controller.BaseController;
import com.xiaoyun.utils.PagedGridResult;
import com.xiaoyun.utils.XYJSONResult;
import com.xiaoyun.vo.OrderStatusCountsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
@Api("用户中心我的订单")
@RequestMapping("myorders")
public class MyOrderController extends BaseController {


    @ApiOperation("查询商品评价等级")
    @GetMapping("/comment")
    public XYJSONResult commentLevel(
            @RequestParam String userId,
            @RequestParam Integer orderStatus,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = Const.COMMENT_PAGE_SIZE) Integer pageSize) {
        if (StringUtils.isBlank(userId)) {
            return XYJSONResult.errorMsg("");
        }
        PagedGridResult gridResult = myOrderService.queryMyOrders(userId, orderStatus, page, pageSize);
        return XYJSONResult.ok(gridResult);
    }

    @ApiOperation("商家发货")
    @GetMapping("/deliver")
    public XYJSONResult deliver(
            @RequestParam String orderId) {
        if (StringUtils.isBlank(orderId)) {
            return XYJSONResult.errorMsg("");
        }
        myOrderService.updateDeliverOrderStatus(orderId);
        return XYJSONResult.ok();
    }

    @ApiOperation("用户确认收获")
    @PostMapping("/confirmReceive")
    public XYJSONResult confirmReceive(
            @RequestParam String orderId,
            @RequestParam String userId
    ) {
        if (StringUtils.isBlank(orderId)) {
            return XYJSONResult.errorMsg("");
        }
        if (StringUtils.isBlank(userId)) {
            return XYJSONResult.errorMsg("");
        }
        XYJSONResult checkUserOrder = checkUserOrder(userId, orderId);
        if (checkUserOrder.getStatus() != HttpStatus.OK.value()) {
            return checkUserOrder;
        }
        boolean result = myOrderService.updateReceiveOrderStatus(orderId);
        if (!result) {
            return XYJSONResult.errorMsg("订单确认收获失败");
        }

        return XYJSONResult.ok();
    }

    @ApiOperation("用户确认删除")
    @PostMapping("/delete")
    public XYJSONResult delete(
            @RequestParam String orderId,
            @RequestParam String userId
    ) {
        if (StringUtils.isBlank(orderId)) {
            return XYJSONResult.errorMsg("");
        }
        if (StringUtils.isBlank(userId)) {
            return XYJSONResult.errorMsg("");
        }
        XYJSONResult checkUserOrder = checkUserOrder(userId, orderId);
        if (checkUserOrder.getStatus() != HttpStatus.OK.value()) {
            return checkUserOrder;
        }

        boolean result = myOrderService.updateReceiveOrderStatus(userId, orderId);
        if (!result) {
            return XYJSONResult.errorMsg("订单确认删除失败");
        }

        return XYJSONResult.ok();
    }

    @ApiOperation(("获取订单状态数概况"))
    @PostMapping("statusCounts")
    public XYJSONResult statusCounts(@RequestParam String userId) {
        if (StringUtils.isBlank(userId)) {
            return XYJSONResult.errorMsg("");
        }
        OrderStatusCountsVO statusCount = myOrderService.getOrderStatusCount(userId);
        return XYJSONResult.ok(statusCount);
    }

    @ApiOperation("查询订单动向")
    @PostMapping("trend")
    public XYJSONResult trend(@RequestParam String userId,
                              @RequestParam(required = false, defaultValue = "1") Integer page,
                              @RequestParam(required = false, defaultValue = Const.COMMENT_PAGE_SIZE) Integer pageSize) {
        if (StringUtils.isBlank(userId)) {
            return XYJSONResult.errorMsg("");
        }
        PagedGridResult gridResult = myOrderService.queryOrderTrend(userId, page, pageSize);
        return XYJSONResult.ok(gridResult);
    }
}
